﻿#include "MDLoginSetting.h"
#include "OptionManager.h"
#include "CTPLoginAccount.h"

MDLoginSetting::MDLoginSetting(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	init();
}

MDLoginSetting::~MDLoginSetting()
{
}

void MDLoginSetting::slotSave()
{
	std::string mdLoginOptFile = OptionManager::Instance().GetCTPMDAccountSettingFile();
	CTPLogin::MDAccount pAccount(QString::fromLocal8Bit(mdLoginOptFile.c_str()));
	pAccount.SetBrokerID(ui.ledt_brokerId->text());
	pAccount.SetUserID(ui.ledt_userId->text());
	pAccount.SetPassword(ui.ledt_pwd->text());
	QStringList urls;
	for (int i = 0; i < ui.list_url->count(); ++i)
	{
		QListWidgetItem *pItem = ui.list_url->item(i);
		urls << pItem->text();
	}
	pAccount.SetUrlList(urls);
}

void MDLoginSetting::init()
{
	std::string mdLoginOptFile = OptionManager::Instance().GetCTPMDAccountSettingFile();
	CTPLogin::MDAccount pAccount(QString::fromLocal8Bit(mdLoginOptFile.c_str()));

	ui.ledt_brokerId->setText(QString::fromLocal8Bit(pAccount.GetBrokerID().c_str()));
	ui.ledt_userId->setText(QString::fromLocal8Bit(pAccount.GetUserID().c_str()));
	ui.ledt_pwd->setText(QString::fromLocal8Bit(pAccount.GetPassword().c_str()));

	std::list<std::string> pUrlList;
	pAccount.GetUrlList(pUrlList);
	ui.list_url->clear();
	for (std::list<std::string>::iterator it = pUrlList.begin(); it != pUrlList.end(); ++it)
	{
		ui.list_url->addItem(QString::fromLocal8Bit(it->c_str()));
	}
}
